/*
 * @Author: stark
 * @Date: 2022-06-28 09:13:36
 */

$(function () {
    $.cookie.json = true;
    let userInfo = $.cookie('userInfo') ? $.cookie('userInfo') : null;
    let goodsIds = $.getUrlParam('goodsIds');

    console.log(goodsIds);
    let data = {
        userId: userInfo.id,
        cartIds: goodsIds
    }

    console.log(data);
    $.get(baseUrl + '/cart/lists', data, res => {

        console.log(res);
        let result = res.data;
        console.log(result);
        let html = '';

        result.forEach(item => {
            html += `
                     <li>
                            <img src="${item.goods.goodsImg}" alt="">
                            <span class="goodsName">${item.goods.goodsName}</span>
                             <span class="shopPrice">￥${item.goodsCount * item.goods.shopPrice}</span>
                             <span class="goodsCount">x${item.goodsCount}</span>
                             <span>有货</span>
                      </li>
            `
        })

        $(".right-content").html(html);

        let goods_Sum = 0
        $(".shopPrice").each(function (i, ele) {
            goods_Sum += parseInt($(ele).text().substr(1));
        })

        $(".goods_Sum").text('￥' + goods_Sum.toFixed(2))

        $(".freight").text(goods_Sum > 10 ? '￥0.00' : '￥10.00')

        let freight = parseInt($(".freight").text().substr(1));

        let handle_Sum = '￥' + (goods_Sum + freight).toFixed(2)
        $(".handle_Sum").text(handle_Sum);

        $(document).on('click', '.site_list li', function () {
            $(".overName").text($(this).find(".addr-info").text())
            $(".overAdd").text($(this).find(".add-name").text())
        })

    })



    let html = ''
    $.get(baseUrl + '/address/lists', { userId: userInfo.id }, res => {
        let result = res.data;

        result.forEach((item, index) => {
            html += `  <li>
                            <div class="consignee-item">
                                    <span>${item.consignee} ${item.provinceName.slice(0, 2)}</span>
                                <b></b>
                                    </div>
                                <div class=" info-item">
                                        <span class="add-name">${item.consignee}</span>
                                        <span class="addr-info">${item.provinceName}  ${item.cityName}
                                        ${item.areaName}  ${item.address}
                                        </span>
                                        <span class="addr-tel">${item.mobile}</span>
                                        ${index === 0 ? '<span class="addr-default">默认地址</span>' : ''}
                                        <span class="addr-update"><a href="javaScript:;">删除</a></span>
                                        <span class="addr-edit"><a href="javaScript:;">编辑</a></span>
                                    </div>
                                </li>`
        })
        $(".site_list").html(html);


        $(".goBack").on('click', function () {
            location.href = 'car.html?id=' + goodsIds

        })


        // 删除地址
        $(document).on('click', '.addr-update', function () {
            let index = $(this).parents("li").index();

            $.get(baseUrl + '/address/remove', { addressId: result[index].id, userId: userInfo.id }, res => {
                location.href = location.href
            })
        })

        // 修改信息弹出层
        $(document).on('click', '.addr-edit', function () {
            let index = $(this).parents("li").index();
            let index2 = layui.layer.open({
                type: 2,
                content: `site.html?id=${result[index].id}`,
                area: ['670px', '458px'],
                title: '修改收货人信息',
            })

        })

    })

    // 收货人信息特效

    $(document).on('mouseover', '.site_list li', function () {
        $(this).children(".consignee-item").addClass("active")
        $(this).children(".info-item").addClass("pink")
        $(this).find(".addr-update").show();
        $(this).find(".addr-edit").show();
    })



    $(document).on('mouseout', '.site_list li', function () {
        $(this).children(".consignee-item").removeClass("active")
        $(this).children(".info-item").removeClass("pink")
        $(this).find(".addr-update").hide();
        $(this).find(".addr-edit").hide();
    })
})